package com.yindian.cos.easy.security.authentication.login.normal;

import com.yindian.cos.easy.security.authentication.AbstractAuthentication;
import com.yindian.cos.easy.security.authentication.login.UserDetail;
import com.yindian.cos.easy.security.exception.NullUserDetailException;

import javax.servlet.http.HttpServletRequest;

/**
 * 表单登录和短信验证码登录的抽象
 *
 * @author zhangchuanhao
 * @create 2018/11/18
 */
public abstract class AbstractLoginAuthentication extends AbstractAuthentication {

    private UserService userService;

    public AbstractLoginAuthentication(UserService userService) {
        this.userService = userService;
    }

    /**
     * 如果父类support(HttpServletRequest request)方法返回true,则执行该方法尝试获取UserDetail
     *
     * @param request
     * @return
     */
    public UserDetail execute(HttpServletRequest request) {
        UserDetail userDetail = userService.getUserDetail(request);
        if (userDetail == null) {
            throw new NullUserDetailException("userDetail不能为空：" + "[" + this.getClass().getName() + "]");
        }
        userDetail.verify();
        return userDetail;
    }
}
